import headerWrap from "../commonComponents/header.vue"
import noData from "../commonComponents/noMoreData.vue"
import foldWrap from "../commonComponents/fold.vue"
import actionSheetWrap from "../commonComponents/actionSheet.vue"
import textarea_y from "../commonComponents/adviseTextarea.vue"
import actionSheet from "../commonComponents/actionSheet.vue"
import leaderAdviseList from "../progressPublic/leaderAdviseList.vue"
import Sortable from 'sortablejs';

export default {
  data() {
    return {
      bodyShow: false,
      editBtn: false,
      editBtnTxt: "编辑",
      actionShow: false,
      leaderAdShow: false,
      sortable: 99,
      details: {},
      ajData: {},
      actionData: [
        {txt: "设为重要", value: 1},
        {txt: "专家研判", value: 2},
        {txt: "删除选题", value: 3},
      ],
      advise: "",
    }
  },
  methods: {
    init() {
      this.titleFlag = _methods.getUrlParam("title") - 0;
      this.id = _methods.getUrlParam("id");
      _methods.headerTitleChange(this.titleFlag);
      window._vrvInit.isPush().then(() => {
        this.getDetail();
      });
    },
    getDetail() {
      this.bodyShow = false;
      let aj = _ajaxUrls.todayBulletin;
      _ajax.noLoading(aj.url + "/" + this.id,
        {},
        (ajData) => {
          let val = ajData.result;

          val.isTodo = window._methods.isTodo(val.approvalUserID);
          if (val.status == 3) {
            val.isTodo = false;
          }

          this.$store.commit("_leaderAdvise", val.approvalList);

          if (val.isTodo) {
            // _methods.getZjList();
            if (window._data.userInfo.roleRank != 1) {
              val.submitFlag = 10;
              val.zbySubmitLabel = "请示意见";
            } else {
              val.submitFlag = 20;
              val.zbySubmitLabel = "处理意见";
            }
          }

          this.details = val;
          _data.todayNewsDetail = val;
          this.bodyShow = true;
        }, aj.type);
    },
    editInfo(event, flag = false) {
      if (this.editBtn || flag) {
        this.sortInfo();
        this.sortable.destroy();
        this.sortable2.destroy();
        this.sortable = 99;
        this.editBtn = false;
        this.editBtnTxt = "编辑";
        _data.headerClick.ret();
      } else {
        if (this.sortable === 99) {
          this.sortable = Sortable.create(document.getElementById("sort"), {
            group: "sort",
            handle: ".drag-wrap",
            draggable: ".draggable",
            animation: 150,
          });
          this.sortable2 = Sortable.create(document.getElementById("sort"), {
            group: "sort2",
            handle: ".drag-wrap",
            draggable: ".draggable2",
            animation: 150,
          });
        }
        this.$store.commit("_headerRightType", {
          val: 1,
          txt: "添加选题"
        });
        this.editBtn = true;
        this.editBtnTxt = "完成";
      }
    },
    deleteNews(item = this.infoChecked, index = this.infoIndex) {
      if (this.details.infoProcessDetails.length === 1) {
        mui.toast("至少需要一条选题");
        return false;
      }
      let type = {
        40: 2,
        50: 1,
      }[this.titleFlag];
      let data = `?messageId=${item.messageId}&type=${type}&processId=${this.details.processId}`;
      _ajax.confirmWithLoading(_ajaxUrls.tdDeleteInfo.url + item.messageId + data,
        {},
        () => {
          this.details.infoProcessDetails.splice(index, 1);
        }, _ajaxUrls.tdDeleteInfo.type, "是否确认删除？");
    },
    goDetails(item) {
      _methods.goPath(`/todayBulletin/infoDetail?id=${item.id}&type=2`);
    },
    submitTn(type) {
      let self = this;
      // advice: "请示领导", //研判意见
      //   approvalUserID: "111111", //请示人ID
      //   type: 1 //1.转批2.同意3.退回
      let data = {
        approvalUserID: "",
        advice: _methods.getValue("advise"),
        type: type,
      };

      if (type === 1) {
        data.approvalUserID = _data.processSubmitData.userIds;
      }
      //
      // if (!data.advice) {
      //   data.advice = {
      //     1: "请示领导",
      //     2: "同意",
      //     3: "退回"
      //   }[type];
      // }

      _ajax.confirmWithLoading(_ajaxUrls.bulletinApproval.url + "/" + this.id,
        data,
        () => {
          _alert.alert(() => {
            _vrvInit.isPushCloseWindow();
            _methods.meScrollTriggerDownScroll(_data.todayNewsPendingList);
            _methods.meScrollTriggerDownScroll(_data.todayNewsCompleteList);
            self.init();
          });
        },
        _ajaxUrls.bulletinApproval.type,
        "是否确认提交?");

    },
    goLeader() {
      _methods.goPeopleList(4, 1);
    },
    infoCheck(item, index) {
      if (item.isImportant == 1) {
        this.actionData[0].txt = "设为一般";
      } else {
        this.actionData[0].txt = "设为重要";
      }
      this.actionShow = true;
      this.infoChecked = item;
      this.infoIndex = index;
    },
    setImportant() {
      // let type = {
      //   40: 4,
      //   100: 3,
      // }[this.titleFlag];
      let im = this.details.infoProcessDetails[this.infoIndex].isImportant;

      im = im == 1 ? 0 : 1;

      let data = {
        id: this.infoChecked.id,
        isImportant: im,
      };
      _ajax.noLoading(_ajaxUrls.todayNewsImportant.url + this.infoChecked.id,
        data,
        () => {
          this.details.infoProcessDetails[this.infoIndex].isImportant = im;
          let item = this.details.infoProcessDetails[this.infoIndex];
          this.details.infoProcessDetails.splice(this.infoIndex, 1);
          if (im == 1) {
            this.details.infoProcessDetails.unshift(item);
          } else {
            this.details.infoProcessDetails[this.details.infoProcessDetails.length] = item;
          }

        }, _ajaxUrls.todayNewsImportant.type);
    },
    sortInfo() {
      // let type = {
      //   40: 4,
      //   100: 3,
      // }[this.titleFlag];

      let data = {
        ids: "",
      };

      let list = document.getElementById("sort").childNodes;

      let idArr = [];

      for (let i = 0; i < list.length; i++) {
        let id = list[i].getAttribute("data-value");
        if (id) {
          idArr.push(id);
        }
      }

      data.ids = idArr.join(",");

      _ajax.noLoading(_ajaxUrls.todayNewsSort.url,
        data,
        () => {

        }, _ajaxUrls.todayNewsSort.type);

    },
    actionCheck(value) {
      switch (value) {
        case 1:
          this.setImportant();
          break;
        case 2:
          _methods.goPath(`/tnDetail/toZj?type=40&id=${this.infoChecked.id}`);
          break;
        case 3:
          this.deleteNews();
          break;
      }
    }
  },
  components: {
    headerWrap,
    noData,
    foldWrap,
    actionSheetWrap,
    leaderAdviseList,
    textarea_y,
    actionSheet,
  },
  created() {
    _vrvInit.isPush().then(() => {
      this.init();
    });
  },
  mounted() {
    let self = this;
    _data.busInit.on_(() => {
      if (self.$store.state.headerRightType.val === 1) {
        _methods.goPath("/tnDetail/infoList");
      }
    }, 1);
    _data.bus.$on("toLeader", () => {
      setTimeout(() => {
        self.submitTn(1);
      }, 100);
    });
    _data.bus.$on("addNews", (v) => {
      this.getDetail();
      this.editInfo(true);
    });
  },
  beforeDestroy() {
    _data.headerClick._off(1);
    if (this.sortable !== 99) {
      this.sortable.destroy();
      this.sortable2.destroy();
    }
    this.$store.commit("_headerRightType", 99);
    _methods.headerTitleChange(1, true);
    _data.bus.$off("toLeader");
    _data.bus.$off("addNews");
  }
}
